/*****************************************************************************/
/*!	\file		MainClient.c
 *	\brief		C Source code file for the Example IEC61850 Client using the YX-PIS
 *				This Client receives data for a CSWI, GGIO, Phase Measurement and a Curve Description Array
 *				via GOOSE and Reporting.
 *				The Client can Direct Operate the GGIO and use Select before Operate to control the CSWI which
 *				is and enhanced security control.
 *
 *	\par		Dalian Yunxing Tech Co., Ltd.
 *
 *				Dalian, China
 *				Phone   : +86 (411) 8825 4852
 *				Email   : yx@yunxing.tech
 */
/*****************************************************************************/

#if defined (__PARADIGM__)
// for BECK we need the clib.h
#include <clib.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MainClient.h"
#include "IEC61850API.h"
#include "PIS10CreateServerClient.h"
#include "IEC61850Functions.h"
#include "LocalData.h"
#include "ExampleTypes.h"
#include "PrintView.h"
#include "UserInput.h"

#ifndef NO_SSL
#include <openssl/ssl.h>
#include <openssl/err.h>
#endif


static char* s_cidFile = NULL;

int main(int argc, char** argv)
{
	char continueMainLoop = 'y';
	enum UserCommands usrCommand = COMMAND_NONE;	// the User command to execute
	IEC61850_ErrorCode IEC61850Error = S_OK;	// variable to hold the IEC61850_ErrorCode
	Boolean boolVal = FALSE;	// temporary value to hold user input

#if defined (__PARADIGM__)
	// for BECK Set STDIO focus to application
	BIOS_Set_Focus(FOCUS_APPLICATION);
#endif

#ifndef NO_SSL
	SSL_library_init();
	OpenSSL_add_all_algorithms();
	SSL_load_error_strings();
#endif

	if (argc >= 2) {
		s_cidFile = argv[1];
	}

	IEC61850Error = setupIEC61850Client();

	if (IEC61850Error == S_OK) {
		do {
			PrintClientFullView();

			usrCommand = GetClientCommandFromUser();

			switch (usrCommand) {
			case COMMAND_EXIT:
				continueMainLoop = 'n'; /* Set the Loop to exit */
				break;
			case COMMAND_MMS_GET_CONNECTED_SERVER_LIST:
				GetConnectionsList();
				break;
			case COMMAND_PRINT_REPORT_MENU:
				ReportOperate();
				break;
			case COMMAND_PRINT_CONTROL_MENU:
				ControlOperateDemo();
				break;
			case COMMAND_PRINT_FILE_MENU:
				FileOperate();
				break;
			case COMMAND_PRINT_SETTING_GROUP_MENU:
				SettingGroupOperate();
				break;
			case COMMAND_PRINT_LOG_MENU:
				LogOperate();
				break;
			case COMMAND_PRINT_DATA_ACCESS_MENU:
				DataAccess();
				break;
			case COMMAND_INNER_UNIT_TEST:
				InnerTestClient();
				break;
			default:
				break;
			}
		} while (continueMainLoop == 'y');
	}

	printf("Stopping IEC61850 Client \n");
	StopMyServerClient();
	printf("Freeing IEC61850 Client \n");
	FreeMyServerClient();

	// Allow the program to pause and let the user see any exit messages
	printf("\nIEC61850 Client Example has ended, press enter to exit\n");
	getchar();

#if defined (__PARADIGM__)
	// for BECK Set STDIO focus back to both for terminal and Applications
	BIOS_Set_Focus(FOCUS_BOTH);
#endif

	return 0;
}

IEC61850_ErrorCode setupIEC61850Client()
{
	IEC61850_ErrorCode result = IEC61850_ERROR_SERVICE_FAILED;

	// ensure that the API header files and the library are version-compatible.
	if (memcmp(IEC61850_GetLibraryVersion(), YX_PIS_VERSION, sizeof(YX_PIS_VERSION)) == 0) {
		result = CreateClient();

		if (result == S_OK) {
			result = LoadSCLFile(s_cidFile ? s_cidFile : "./mms-cid/client.cid");
			if (result == S_OK) {
				result = SetControlsOrCat();
				result = StartMyServerClient();
			} else if (result == IEC61850_ERROR_LICENCE_INVALID) {
				printf("IEC61850 License for this device is invalid\n");
			}
		} else if (result == IEC61850_ERROR_LICENCE_INVALID) {
			printf("IEC61850 License for this device is invalid\n");
		}
	} else {
		printf("Version Mismatch: YX-PIS IEC61850API.h Version = %s , YX-PIS Library Version = %s\n", YX_PIS_VERSION, IEC61850_GetLibraryVersion());
	}
	return result;
}
